<?php

namespace common\models;

use common\config\TimeFormator;
use Yii;
use yii\web\UploadedFile;

/**
 * This is the model class for table "ico_project".
 *
 * @property string $id
 * @property string $name
 * @property string $base_desc
 * @property string $team_desc
 * @property string $white_book
 * @property string $code
 * @property string $publish_time
 * @property string $cankao_price
 * @property string $platform
 * @property string $allcount
 * @property string $count
 * @property string $end_time
 * @property string $start_time
 * @property string $level
 * @property string $created_at
 * @property string $updated_at
 * @property string $unit
 * @property string $cover
 * @property string $fee
 * @property string $current_count
 * @property string $left_count
 * @property string $recieve_address
 * @property IcoOrder $orders
 */
class IcoProject extends \common\models\BaseActiveRecord
{
    /**
     * @var UploadedFile
     */
    public $coverFile;

    const STATUS_NOT_START = 0;

    const STATUS_ING = 1;

    const STATUS_OVER = 2;

    const STATUS_COMPLETE = 3;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ico_project';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name','code','white_book'], 'required'],
            [['id', 'created_at', 'updated_at'], 'integer'],
            [['base_desc','level', 'team_desc', 'current_count','left_count','end_time', 'recieve_address', 'start_time',  'white_book'], 'safe'],
            [['count'], 'number'],
            [['name', 'code', 'allcount'], 'string', 'max' => 100],
            [['publish_time', 'cankao_price'], 'string', 'max' => 50],
            [['platform', 'unit', 'cover', 'fee'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '项目id',
            'name' => '项目名称',
            'base_desc' => '项目介绍',
            'team_desc' => '团队介绍',
            'white_book' => '白皮书',
            'code' => '代码',
            'publish_time' => '发币时间',
            'cankao_price' => '参考价格',
            'platform' => '上线平台',
            'allcount' => '总量',
            'count' => '募集量',
            'end_time' => '结束时间',
            'start_time' => '开始时间',
            'level' => '评级',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'unit' => '发币币种',
            'cover' => '封面图',
            'fee' => '手续费',
            'left_count' => '剩余数量',
            'current_count' => '已经募集数量',
            'recieve_address' => '收币地址',
        ];
    }

    /**
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(is_string($this->start_time)){
            $this->start_time = TimeFormator::getFormatTimestamp($this->start_time);
        }
        if(is_string($this->end_time)){
            $this->end_time = TimeFormator::getFormatTimestamp($this->end_time);
        }
        if(!empty($this->coverFile)){
            $path = __DIR__ . '/../../backend/web';
            $filePath = '/upload/image/'.date('Ymd');
            if(!is_dir(sprintf('%s/%s',$path,$filePath))){
                mkdir(sprintf('%s/%s',$path,$filePath),0777);
            }
            $filename = $this->coverFile->name . time();
            $fn = sprintf('%s/%s/%s.%s',$path,$filePath,$filename,$this->coverFile->extension);
            if ($this->coverFile->saveAs($fn)) {
                $this->cover = sprintf('%s/%s.%s',$filePath,$filename,$this->coverFile->extension);
            }
//            $key = 'cover/' . md5('ico:' . time());
//            if (Yii::$app->uploader->uploaderFile($this->coverFile, ['key' => $key])) {
//                $this->cover = $key;
//            }
        }
        return parent::beforeSave($insert);
    }

    public function getOrders()
    {
        return $this->hasMany(IcoOrder::className(),['ico_project_id'=>'id']);
    }
}
